<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<form class="layui-form" action="" lay-filter="varItem" onsubmit="return false">
    <div class="layui-row" style="margin-top: 15px;padding-right: 35px;">
        <div class="layui-form-item item-first">
            <label class="layui-form-label">变量名称</label>
            <div class="layui-input-inline">
                <input type="text" id="varName" name="varName" lay-verify="required" autocompvare="off" placeholder="请输入变量名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">变量key</label>
            <div class="layui-input-inline">
                <input type="text"  id="varKey" name="varKey" lay-verify="required" autocompvare="off" placeholder="请输入变量key" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">数据类型</label>
            <div class="layui-input-inline">
                <select name="varDataType" lay-filter="varDataType" lay-verify="required">
                    <option value="varchar">字符串(varchar)</option>
                    <option value="number">数字(number)</option>
                    <option value="date">日期(date)</option>
                </select>
            </div>
        </div>

        <input type="hidden" id="varId" name="varId" >
        <input type="hidden" id="modelId" name="modelId" >
    </div>
    <div class="layui-btn-container" style="text-align: center;position: absolute;bottom: 10px;left: 0px;right: 0px;">
        <button class="layui-btn" lay-submit lay-filter="varForm" type="button">保存</button>
        <button class="layui-btn layui-btn-danger" id="return" type="button">取消</button>
    </div>
</form>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form','jquery','setter','commons' ], function () {
        var form = layui.form;
        var $ = layui.$;
        var setter = layui.setter;
        var commons = layui.commons;
        var modelId = '${modelId}';
        var varId = '${varId}';
        $("#modelId").val(modelId);
        if(varId!=null && varId!=""){
            $.ajax({
                url: '${ctx}/workflow/bpmDefVar/detail?varId='+varId,
                dataType: "json",
                success: function (result) {
                   // alert(result);
                    if (commons._filterRecord(result)) {
                        //表单初始赋值
                        var returnData = result.data;
                        form.val('varItem',{
                            "varId": returnData.varId,
                            "modelId": returnData.modelId,
                            "varName": returnData.varName,
                            "varKey": returnData.varKey,
                            "varDataType": returnData.varDataType
                        });
                    } else {
                        window.parent.layer.alert(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                    window.parent.layer.alert("保存失败！" + XMLHttpRequest);
                }
            });
        }
        var thisIframeIndex = window.parent.layer.getFrameIndex(window.name);
        $('#return').click(function () {
            window.parent.layer.close(thisIframeIndex);
        });

        //监听提交
        form.on('submit(varForm)', function (data) {
            //验证变量名称和变量key
            $.ajax({
                    url: "${ctx}/workflow/bpmDefVar/save",
                    type: "POST",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify(data.field),
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                            parent.layer.close(thisIframeIndex);
                            parent.layui.table.reload('variableTable',{page:{curr:1}});
                        } else {
                            window.parent.layer.alert(data.msg);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        window.parent.layer.alert("保存失败！" + XMLHttpRequest.status);
                    }
            });
        });

        //自动生成别名
        $("#varName").change(function () {
            var str = $(this).val();
            if (str == "") return;
            var arrRslt = component.chineseToPinyinAlias(str);
            $("#varKey").val(arrRslt[0]);
        });
    });
</script>
</body>
</html>
